package com.sanjiahantang.youjiarole.module.login.views.impl;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.module.common.presenters.ChangePasswordPresenter;
import com.sanjiahantang.youjiarole.module.common.views.ChangePasswordView;
import com.sanjiahantang.youjiarole.utils.KeyBoardUtils;


/**
 * Created by sks on 2015/9/17.
 * 修改密码
 */
public class ChangePasswordActivity extends BaseMvpActivity<ChangePasswordView,ChangePasswordPresenter> implements ChangePasswordView,View.OnClickListener,TextWatcher {
    TextView title;
    private Context context;
    private EditText editOldPassword,editNewPassword,editAgain;
    private TextView btnSure;
    private String oldPass,newPass,again;
    private static int INTENT_PAY = 0x6;
    private int type;


    @Override
    public ChangePasswordPresenter initPresenter() {
        return new ChangePasswordPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_changepassword);
        context = this;
        title=(TextView)findViewById(R.id.title);
        title.setText("修改密码");
        findViewById(R.id.fl_Left).setOnClickListener(this);
        editOldPassword = (EditText) findViewById(R.id.editOldPassword);
        editNewPassword = (EditText) findViewById(R.id.editNewPassword);
        editAgain = (EditText) findViewById(R.id.editAgain);
        btnSure = (TextView) findViewById(R.id.btnSure);
        editOldPassword.addTextChangedListener(this);
        editNewPassword.addTextChangedListener(this);
        editAgain.addTextChangedListener(this);
        type = getIntent().getIntExtra(KEY_TYPE,0);
        if (type == INTENT_PAY){
            changeType(editOldPassword);
            changeType(editNewPassword);
            changeType(editAgain);
        }
    }

    private void changeType(EditText et){
        InputFilter[] inputFilter = new InputFilter[]{new InputFilter.LengthFilter(6)};
        et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
        et.setFilters(inputFilter);
    }

    @Override
    public void initData() {

    }


    private void submit(){
        if(!newPass.equals(again)){
            Utils.showToast(context,"两次输入的密码不一致");
            return;
        }
        if (type == INTENT_PAY){
            presenter.modifyPayPwd(this,oldPass,newPass, new OnLoadListener() {
                @Override
                public void onLoad(Object content) {

                }
            });
        }else{
            presenter.submit(this,oldPass,newPass);
        }

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_Left:
                KeyBoardUtils.closeKeybord(this);
                break;
            case R.id.btnSure:
                submit();
                break;
        }
    }



    @Override
    public void showProgress() {

    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        oldPass = editOldPassword.getText().toString().trim();
        newPass = editNewPassword.getText().toString().trim();
        again = editAgain.getText().toString().trim();
        if(!oldPass.equals("") && !again.equals("")
                && !newPass.equals("")){

            btnSure.setClickable(true);
            btnSure.setOnClickListener(ChangePasswordActivity.this);
        }else{
            btnSure.setClickable(false);
        }
    }
    @Override
    public void afterTextChanged(Editable editable) {
    }

    public static void changeLoginPsd(Context context){
        context.startActivity(new Intent(context,ChangePasswordActivity.class));
    }

    public static void changePayPsd(Context context){
        context.startActivity(new Intent(context,ChangePasswordActivity.class).putExtra(KEY_TYPE,INTENT_PAY));
    }
}
